package com.awspaas.user.apps.cpq.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.cpq.util.RefreshDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshModulDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshTempDataUtils;

public class UpdatePropertiesJob implements IJob{

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			/**
			 * 更新原材料价格维护表
			 */
			List<BO>bos=SDK.getBOAPI().query("BO_EU_CPQ_PROPERTIES").addQuery("SFTJ=","1").list();
			if(bos.size()>0) {
				// 获取需要更新的原材料编号。
			    Map<String, JSONObject> userMap = new HashMap<String, JSONObject>();
			    String bms = "";
				List<String> bhList = new ArrayList<>();
				for (BO bo : bos) {
					String bm =bo.getString("CLBM");
					//String status = (String) jsonObject.get("STATUS");

						JSONObject json = new JSONObject();
						json.put("CLDJ", bo.getString("BHSCGJ"));
						json.put("MC", bo.getString("CLMC"));
						json.put("LX", bo.getString("LX"));
						json.put("DW", bo.getString("CLDW"));
						json.put("XH", bo.getString("GG"));
						json.put("BM", bm);
						bhList.add(bm);
						userMap.put(bm, json);
						SDK.getBOAPI().updateByBindId("BO_EU_CPQ_PROPERTIES", bo.getBindId(), "SFTJ", "");
						
				}
				/**
				 * 1、获取需要更新的制造件信息
				 */
			Map<String, JSONObject> comtMap=RefreshDataUtils.getManufacturedPart(userMap);
				/**
				 * 2、获取需要更新的装配件信息
				 */
			Map<String, JSONObject> comtMapNew=RefreshDataUtils.getAssembly(comtMap);
			//将更新的物料信息存放在comtMapNew中
			if(comtMap.size()>0) {
				for (String  key : comtMap.keySet()) {
					comtMapNew.put(key, comtMap.get(key));
				}
			}
			
			
				/**
				 * 3、获取需要更新的模块信息
				 * 
				 */
			Map<String, Map<String, Object>> modulLists=RefreshModulDataUtils.getPorpertiesModule(comtMapNew);
				/**
				 * 4、获取需要更新的项目报价记录台账中类型为模板的数据
				 * 
				 */
			RefreshTempDataUtils.upateTempByPorperties(modulLists);
			}
		} catch (Exception e) {
			throw e;
		}
		
	}

}
